package sort;

public class MergeSort {
    public static void main(String[] args) {
        int[] array = new int[]{2, 3, 1, 0, 4, 5};
        mergeSort(array, 0, array.length - 1);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

    public static void mergeSort(int[] nums, int l, int r){
        if(l >= r) return;

        int mid = (l + r)/ 2;
        mergeSort(nums, l, mid);
        mergeSort(nums, mid+1, r);

        int i = l, j = mid+1, k = 0;
        int[] tmp = new int[r-l+1];
        while (i <= mid && j <= r){
            tmp[k++] = nums[i] <= nums[j] ? nums[i++] : nums[j++];
        }
        while (i <= mid) tmp[k++] = nums[i++];
        while (j <= r) tmp[k++] = nums[j++];

        for(k = 0, i = l; i <= r; ) nums[i++] = tmp[k++];
    }
}
